package gov.tax.rti.base.jdbc.impl;

import gov.tax.rti.base.jdbc.BaseJdbcDaoSupport;
import gov.tax.rti.base.util.DbBaseConstants;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

@Repository
public class DataMapJdbcImpl extends BaseJdbcDaoSupport{


	public Map<String,Object> getMapSqls(String sqlKey) {
		Map<String, Object> mapSqls = new HashMap<String, Object>();
		try{
			mapSqls = getJdbcTemplate().queryForMap("select t.MAPVALUE,t.JOINVALUE,t.DATASOURCE from "+DbBaseConstants.TABLE_NAME_DATAMAP+" t where t.MAPKEY = ?", sqlKey);
		}catch (EmptyResultDataAccessException e) {
		}
		return mapSqls;
	}
	
	public List<Map<Object, Object>> getSqlMapping(String sql, String dataSource) {
		try{
			return getJdbcTemplate(dataSource).query(sql, new KVRowMapper());
		}catch(Exception e){
			return null;
		}
	}
	
	public List<Map<Object, Object>> getSqlMapping(String sql,Map<String,Object> paras, String dataSource) {
		return getNamedParameterJdbcTemplate(dataSource).query(sql, paras, new KVRowMapper());
	}
	
	protected class KVRowMapper implements RowMapper<Map<Object, Object>> {
		public Map<Object, Object> mapRow(final ResultSet rs, final int arg1) throws SQLException {
			Map<Object, Object> data = new LinkedHashMap<Object, Object>();
			data.put("key", rs.getObject(1));
			data.put("value", rs.getObject(2));
			return data;
		}
	}

}
